package com.haois.mp.database.module.offaccounts.rest;

import com.github.pagehelper.PageHelper;
import com.haois.mp.database.commom.enums.ApiResultEnum;
import com.haois.mp.database.commom.constant.Reply;
import com.haois.mp.database.commom.enums.MpTypeEnum;

import com.haois.mp.database.module.common.daomain.UserToken;
import com.haois.mp.database.module.common.daomain.Driver;
import com.haois.mp.database.module.common.daomain.Peccancy;
import com.haois.mp.database.module.common.daomain.Vehicle;
import com.haois.mp.database.module.sdk.util.JuHeUtil;
import com.haois.mp.database.module.offaccounts.service.RemindVehicleService;
import com.haois.mp.database.module.offaccounts.service.VehicleService;
import com.haois.mp.database.module.common.service.IUserTokenService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * description
 *
 * @author RuiHaoZ on Hgits.cn
 * @date 2018/9/6 10:33
 */
@RestController
@RequestMapping("/wxmp/rest/driver")
public class DriverRestController {

    @Autowired
    private VehicleService vehicleService;
    @Autowired
    private RemindVehicleService remindVehicleService;
    @Autowired
    private IUserTokenService iUserTokenService;

    @RequestMapping("/wzList")
    public Reply getWzList(HttpServletRequest request, String plate, String beginTime, String endTime) {
        beginTime = beginTime.trim();
        endTime = endTime.trim();
        String access_token = (String) request.getAttribute("access_token");
        UserToken userToken = iUserTokenService.findByAccessToken(access_token, MpTypeEnum.OFFACCOUNTS.getType());

        List<Peccancy> peccancyList = null;
        PageHelper.startPage(request);
        peccancyList = vehicleService.findPeccancyBy(userToken.getUserId(), null, plate, beginTime, endTime);
        return new Reply(ApiResultEnum.API_00, peccancyList);
    }

    @RequestMapping("/driverList")
    public Reply getDriverList(HttpServletRequest request) {
        String access_token = (String) request.getAttribute("access_token");
        UserToken userToken = iUserTokenService.findByAccessToken(access_token, MpTypeEnum.OFFACCOUNTS.getType());
        PageHelper.startPage(request);
        List<Driver> drivers = vehicleService.findDriver(userToken.getUserId());
        return new Reply(ApiResultEnum.API_00, drivers);
    }

    @RequestMapping("/vehicleList")
    public Reply getVehicleList(HttpServletRequest request) {
        String access_token = (String) request.getAttribute("access_token");
        UserToken userToken = iUserTokenService.findByAccessToken(access_token, MpTypeEnum.OFFACCOUNTS.getType());
        PageHelper.startPage(request);
        List<Vehicle> drivers = vehicleService.findVehicle(userToken.getUserId());
        return new Reply(ApiResultEnum.API_00, drivers);
    }

    @RequestMapping(value = "/driver/updateInfo", method = RequestMethod.POST,
            params = {"name", "register", "dossier", "licenseNo"})
    public Reply addDriver(HttpServletRequest request, Driver driver) {
        String access_token = (String) request.getAttribute("access_token");
        UserToken userToken = iUserTokenService.findByAccessToken(access_token, MpTypeEnum.OFFACCOUNTS.getType());
        Reply reply = vehicleService.insertDriver(driver, userToken.getUserId());
        if (reply.getCode().equals(ApiResultEnum.API_00.getCode())) {
            remindVehicleService.driverValidPeriod((Driver) reply.getData(), userToken.getUserId());
        }
        return reply;
    }

    @RequestMapping(value = "/vehicle/updateInfo", method = RequestMethod.POST,
            params = {"plate", "vin", "type", "engineNo", "register"})
    public Reply addVehicle(HttpServletRequest request, Vehicle vehicle) {
        vehicle.setCity(JuHeUtil.getCityJc(vehicle.getPlate()));
        String access_token = (String) request.getAttribute("access_token");
        UserToken userToken = iUserTokenService.findByAccessToken(access_token, MpTypeEnum.OFFACCOUNTS.getType());
        Reply reply = vehicleService.insertVehicle(vehicle, userToken.getUserId());
        if (reply.getCode().equals(ApiResultEnum.API_00.getCode())) {
            remindVehicleService.vehicleValidPeriod((Vehicle) reply.getData(), userToken.getUserId());
        }
        return reply;
    }

    @RequestMapping(value = "/vehicle/info/{id}", method = RequestMethod.GET)
    public Reply getVehicle(@PathVariable Integer id) {
        Vehicle ve = new Vehicle();
        ve.setId(id);
        Vehicle vehicle = vehicleService.getVehicle(ve);
        return new Reply<>(ApiResultEnum.API_00, vehicle);
    }

    @RequestMapping(value = "/driver/info/{id}", method = RequestMethod.GET)
    public Reply getDriver(@PathVariable Integer id) {
        Driver dr = new Driver();
        dr.setId(id);
        Driver driver = vehicleService.getDriver(dr);
        return new Reply<>(ApiResultEnum.API_00, driver);
    }

    @RequestMapping(value = "/wz/info/{id}", method = RequestMethod.GET)
    public Reply getWz(@PathVariable Integer id) {
        Peccancy peccancy = vehicleService.getPeccancyById(id);
        return new Reply<>(ApiResultEnum.API_00, peccancy);
    }
}
